package GUI; 
import javax.swing.*;

import model.*;
import model.Map;
import order.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class OrderPanel extends JPanel{
	  private Territory territory; 
	  private String[] TechnologyName = {"Infantry:", "Automatic Weapons:", "Rocket Launchers:", "Tanks:", "Improved Tanks:", "Fighter Planes:"};
	  private JLabel technologyLevel; 
	  private JLabel[] troopDisplay;
	  private JButton[] upgradeTroop;
	  private JButton upgradeTechnologyLevel, createMove, createAttack;
	  private JLabel foodunit, technologyunit;
	  private JLabel owner;
	  
	  private HistoryPanel historyPanel;
	  private Map map;
	  private Player player;
  
       public OrderPanel(HistoryPanel _historyPanel, Map _map, Player _player){
		   
    	   historyPanel = _historyPanel;
    	   map = _map;
    	   player = _player;
    	   
		   setLayout(new GridLayout(6, 4, 10, 10));	  
		   setBorder (BorderFactory.createTitledBorder(
					BorderFactory.createEtchedBorder (), "Territory Information"));
		   
		   owner = new JLabel("Owner: N/A");
	  	   technologyLevel = new JLabel("Level: N/A");
	  	   upgradeTechnologyLevel = new JButton("Upgrade Level");
	  	   upgradeTechnologyLevel.addActionListener(new upgradeTechnologyLevelListener(this));
	  	   add(owner);
	  	   add(technologyLevel);
	  	   add(upgradeTechnologyLevel);
	  	   add(new JLabel("    "));
		   
		   foodunit = new JLabel("Food Unit: 0");
	  	   technologyunit = new JLabel("Technology Unit: 0");
	  	   add(foodunit);
	  	   add(new JLabel("    "));
	  	   add(technologyunit);
	  	   add(new JLabel("    "));
	 
	  	   troopDisplay = new JLabel[6];
	  	   upgradeTroop = new JButton[6];
	  	   
	  	   for (int i = 0; i < 6; i++){
	  		   troopDisplay[i] = new JLabel(TechnologyName[i] + "  0(0)");
	  	       add(troopDisplay[i]);
	  	       upgradeTroop[i] = new JButton("Upgrade");
	  	       upgradeTroop[i].addActionListener(new upgradeTroopListener(i));
	  	       add(upgradeTroop[i]);
	  	   }
		   
	  	   upgradeTroop[5].setEnabled(false);
	  	   add(new JLabel("    "));
	  	   createMove = new JButton("Create Move Order");
	  	   add(createMove);
	  	   createMove.addActionListener(new createMoveListener());
	  	   createAttack = new JButton("Create Attack Order");
	  	   add(createAttack);
	  	   createAttack.addActionListener(new createAttackListener());
	  	   add(new JLabel("    "));
       }
	   
	  public void update(Territory t){
          territory = t; 	
		  for (int i = 0; i < 6; i++)
           		troopDisplay[i].setText(TechnologyName[i] + territory.getTroopAmount(i) + "(" + territory.getUsableTroopAmount(i) + ") ");
	        owner.setText("Owner: " + territory.getOwner().getName());
	        technologyLevel.setText("Level: " + territory.getOwner().getTechnologyLevelName());
           	foodunit.setText("Food Unit: " + territory.getFoodUnit());
	        technologyunit.setText("Technology Unit: " + territory.getTechnologyUnit());
	    	  for (int i = 0; i < 6; i++)
	    		  upgradeTroop[i].setEnabled(t.getOwner() == player);
	              
	           createMove.setEnabled(t.getOwner() == player);
	           createAttack.setEnabled(t.getOwner() == player);
	      }
	  
	  public Territory getTerritory(){return territory;}
	  
	  class upgradeTroopListener implements ActionListener{
		  int index;
		
		  public upgradeTroopListener(int i){
			index = i;  
		  }
		  public void actionPerformed(ActionEvent arg0) {
			UpgradeOrderFrame fr = new UpgradeOrderFrame(historyPanel, territory, index);
		}
	  }
	  
	  class upgradeTechnologyLevelListener implements ActionListener{
		  private OrderPanel orderPanel;
		  public upgradeTechnologyLevelListener(OrderPanel _orderPanel){
			  orderPanel = _orderPanel;
		  }
		  public void actionPerformed(ActionEvent arg0) {
				UpgradeTechnologyLevel currentOrder = new UpgradeTechnologyLevel(territory.getOwner());			
			    if (currentOrder.checkOrder()){
			    	historyPanel.addHistory(currentOrder);
			    	currentOrder.excecuteOrder();
			    	orderPanel.update(territory);
			    }
			    	upgradeTechnologyLevel.setEnabled(false);
			}
	  }
	  
	  class createMoveListener implements ActionListener{
		  public void actionPerformed(ActionEvent arg0) {
			  MoveFrame mf = new MoveFrame(historyPanel, territory, map);
				
		  }
	  }
	  
	  class createAttackListener implements ActionListener{
		  public void actionPerformed(ActionEvent arg0) {
			  AttackFrame mf = new AttackFrame(historyPanel, territory, map);
					
			}
	  }
}